import axios from 'axios'

const instance = axios.create({ baseURL: '/api' })

// ✅ 添加请求拦截器
instance.interceptors.request.use(
  config => {
    console.log('🚀 发起请求:', {
      method: config.method,
      url: (config.baseURL || '') + config.url,
      headers: config.headers,
      params: config.params,
      data: config.data,
    })
    return config
  },
  error => {
    console.error('❌ 请求错误:', error)
    return Promise.reject(error)
  }
)

// 添加响应拦截器
instance.interceptors.response.use(
  result => {
    return result.data
  },
  err => {
    alert('服务异常')
    return Promise.reject(err) // 异步状态转化为失败的状态
  },
)

export default instance
